package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Locale;

@WebServlet(name = "SignOnServlet", value = "/SignOnServlet")
public class SignOnServlet extends HttpServlet {
    private static final String MAIN = "/WEB-INF/jsp/catalog/main.jsp";
    private static final String SIGNONFORM = "/WEB-INF/jsp/account/SignOnForm.jsp";

    private Account account;
    private AccountService accountService;
    private LogService logService = new LogService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String checkCode = request.getParameter("checkCode");

//        System.out.println(+"获取到的验证码为：");
        String type = request.getParameter("type");

        Account accountTemp = new Account();
        accountTemp.setUsername(username);
        accountTemp.setPassword(password);
        accountService = new AccountService();
        account = accountService.AccountLogon(accountTemp);

//        System.out.println("account:"+account.getAddress1());
        HttpSession session = request.getSession();
        session.setAttribute("account", account);
        session.setAttribute("tempUserName","");
        session.setAttribute("tempPassword","");
        String checkCode_session =(String) session.getAttribute("checkCode_session");
        Account account = (Account) session.getAttribute("account");
        //验证码输入是否正确，若不正确，刷新页面并告诉用户

        if(checkCode==null)
        {
            session.setAttribute("tempUserName",username);
            session.setAttribute("tempPassword",password);
            session.setAttribute("messageSignOn","checkcode can't be empty!");
            request.getRequestDispatcher(SIGNONFORM).forward(request,response);
        }
        else
        {
            if(checkCode.equalsIgnoreCase(checkCode_session) ){
                System.out.println("验证成功");
//        验证账号是否存在
                if(account == null){
                    session.setAttribute("messageSignOn","Invalid username or password. fail to signIn");
                    session.setAttribute("account",null);
                    request.getRequestDispatcher(SIGNONFORM).forward(request,response);
                }else {

                    session.setAttribute("account",account);
                    session.setAttribute("messageSignOn","");
                    session.setAttribute("tempUserName","");
                    session.setAttribute("tempPassword","");
                    String logInfo = logService.logInfo(" ") + " 用户登录";
                    logService.insertLogInfo(account.getUsername(), logInfo);
                    request.getRequestDispatcher(MAIN).forward(request,response);
                }
            }
            else {
                session.setAttribute("tempUserName",username);
                session.setAttribute("tempPassword",password);
                session.setAttribute("messageSignOn","Invalid checkCode,try it again");
                request.getRequestDispatcher(SIGNONFORM).forward(request,response);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
